import QtQuick 2.12
import QtQuick.Controls 2.5
import AIClient.module 1.0
import QtQuick.Dialogs 1.3
import QtQuick.Layouts 1.12

Page {
    id: dataset_grant_user_page
    width: parent.width*0.8
    height: parent.height*0.8

    title: qsTr("数据集授权用户")
    anchors.fill: parent
    property string ds_id: ""
    property bool m_pb: false
    property int m_role: 3

    signal delGrant(string dataset_id, string grant_id)
    signal changeUserRole(string email, string dataset_id, int role)
    signal error(string msg)
    signal backDataset()
    signal changeDatasetPublic(string dataset_id, int ipb)

    MessageDialog {
        id: delUserDialog
        icon: StandardIcon.Question
        modality: Qt.ApplicationModal
        standardButtons: StandardButton.No | StandardButton.Yes
        title: "删除用户"
        text: "是否将该用户移出数据集?"
        property string grant_id: ""
        property string dataset_id: ""
        onAccepted: {
            console.log("And of course you could only agree.")
        }
        onYes: {
            delGrant(dataset_id, grant_id)
        }
    }

    signal onUpdateGrants()
    //onUpdateGrants: updateGrant()
//    function updateDataset() {
//        removeAll()
//        client.setConfig(m_token)
//        var res = client.list_dataset()
//        var jres = JSON.parse(res)
//        if ( jres["code"] !== 0 )
//        {
//            tip.showmsg(jres["message"])
//            return
//        }
//        tip.showmsg("获取数据集成功")
//        var data = jres["data"]
//        for ( var i=0; i<data.length ; i++ )
//        {
//            data[i]["uid"] = data[i]["id"]
//            data[i]["create_date"] = data[i]["create_date"]//.substring(0,19).replace(/T/," ")
//            addItem(jres["data"][i])
//        }
//    }

    function addItem(item) {
        grantModel.append(item)
    }

    function removeAll() {
        grantModel.clear()
    }

    function removeItem(item) {
        grantModel.remove(item)
    }

    ListModel {
        /* 数据项 */
        id: grantModel
        ListElement {
            uid: "uuid";
            grant_user_id: "red";
            create_date: "2020";
            role_text: "admin";
            role: 0;
            email: "email";
            dataset_id: "modelid";
        }
    }

    ListModel {
        /* 数据项 */
        id: roleModel
        ListElement {
            role: 0;
            text: "创建者";
            color: "black";
        }
        ListElement {
            role: 1;
            text: "管理员";
            color: "black";
        }
        ListElement {
            role: 2;
            text: "编辑者";
            color: "black";
        }
        ListElement {
            role: 3;
            text: "使用者";
            color: "black";
        }
    }

    ListView {
        id:listView
        model: grantModel
        anchors.centerIn: parent
        width: dataset_grant_user_page.width*0.8
        height: dataset_grant_user_page.height*0.8
        header:
            RowLayout {
            spacing: 0
            Rectangle {
                Layout.fillWidth: true
                Layout.minimumWidth: 5
                Layout.preferredWidth: 5
                Layout.preferredHeight: 80
//                height: 80
//                width: 120
                color: "azure"
                Text {
                    anchors.centerIn: parent
                    //width: 120
                    text: "id"
                    visible: false
                    font.pixelSize: Qt.application.font.pixelSize*1.2
                }
            }
            Rectangle {
                Layout.fillWidth: true
                Layout.minimumWidth: listView.width*0.2
                Layout.preferredWidth: listView.width*0.2
                Layout.preferredHeight: 80
//                height: 80
//                width: 180
                color: "azure"
                Text {
                    text: "用户"
//                    width: 180
                    anchors.centerIn: parent
                    //anchors.fill: parent
                    //anchors.right: parent.right
                    font.pixelSize: Qt.application.font.pixelSize*1.2
                }
            }
//            Rectangle {
//                Layout.fillWidth: true
//                Layout.minimumWidth: listView.width*0.25
//                Layout.preferredWidth: listView.width*0.25
//                Layout.preferredHeight: 80
////                height: 80
////                width: 200
//                color: "azure"
//                Text {
//                    text: "创建日期"
////                    width: 200
//                    anchors.centerIn: parent
//                                //anchors.centerIn: parent
//                    //anchors.fill: parent
//                    //anchors.right: parent.right
//                    font.pixelSize: Qt.application.font.pixelSize*1.2
//                }
//            }
            Rectangle {
                Layout.fillWidth: true
                Layout.minimumWidth: listView.width*0.5
                Layout.preferredWidth: listView.width*0.5
                Layout.preferredHeight: 80
//                height: 80
//                width: 200
                color: "azure"
                Text {
                    text: "角色"
//                    width: 200
                    anchors.centerIn: parent
                                //anchors.centerIn: parent
                    //anchors.fill: parent
                    //anchors.right: parent.right
                    font.pixelSize: Qt.application.font.pixelSize*1.2
                }
            }

            Rectangle {
                Layout.fillWidth: true
                Layout.minimumWidth: listView.width*0.1
                Layout.preferredWidth: listView.width*0.1
                Layout.preferredHeight: 80
//                height: 80
//                width: 160
                color: "azure"
                Text {
                    text: "操作"
//                    width: 180
                    anchors.centerIn: parent
                                //anchors.centerIn: parent
                    //anchors.fill: parent
                    //anchors.right: parent.right
                    font.pixelSize: Qt.application.font.pixelSize*1.2
                }
            }
            Rectangle {
                Layout.fillWidth: true
                Layout.minimumWidth: listView.width*0.05
                Layout.preferredWidth: listView.width*0.05
                Layout.preferredHeight: 80
//                height: 80
//                width: 160
                color: "azure"
                Text {
                    text: "公有"
//                    width: 180
                    anchors.centerIn: parent
                                //anchors.centerIn: parent
                    //anchors.fill: parent
                    //anchors.right: parent.right
                    font.pixelSize: Qt.application.font.pixelSize*1.2
                    visible: m_role<=0
                }
            }
            Rectangle {
                Layout.fillWidth: true
                Layout.minimumWidth: listView.width*0.05
                Layout.preferredWidth: listView.width*0.05
                Layout.preferredHeight: 80
//                height: 80
//                width: 160
                color: "azure"
                Switch {
                    id: pswitch
                    anchors.left: parent.left
                    anchors.verticalCenter: parent.verticalCenter
                    checked: m_pb
                    onClicked: {
                        changeDatasetPublic(ds_id, pswitch.checked)
                    }
                    visible: m_role<=0

//                    leftTip: "Public"
//                    rightTip: "Private"
                }

//                Text {
//                    text: "Public"
////                    width: 180
//                    anchors.centerIn: parent
//                                //anchors.centerIn: parent
//                    //anchors.fill: parent
//                    //anchors.right: parent.right
//                    font.pixelSize: Qt.application.font.pixelSize*1.2
//                }
            }
            Rectangle{
                Layout.fillWidth: true
                Layout.minimumWidth: listView.width*0.1
                Layout.preferredWidth: listView.width*0.1
                Layout.preferredHeight: 80
                color: "azure"
//                    height: 80
//                    width: 80
                OrangeButton {
                    anchors.centerIn: parent
                    text: "返回数据集列表"

//                icon.source:  "/images/images/add.jpeg"
//                icon.height: 80
//                icon.width: 80
//                icon.color: transparent
//                flat: true
                    onClicked: {
                        backDataset()
                        return
//                        dataset_create.title = "创建数据集"
//                        dataset_create.m_uid = ""
//                        dataset_create.m_mode = 1
//                        dataset_create.m_dataset_path = ""
//                        dataset_create.m_dataset_name = ""
//                        dataset_create.m_dataset_description = ""
//                        dataset_create.setToken(m_token)
//                        dataset_create.updateMode()
//                        dataset_create.show()

                    }
                }
            }
        }
        delegate: ItemDelegate {
            height: 80
            width: parent.width
            RowLayout {
                spacing: 0
                Rectangle {
                    Layout.fillWidth: true
                    Layout.minimumWidth: 5
                    Layout.preferredWidth: 5
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 120
                    Text {
                        anchors.centerIn: parent
//                        width: 120
                        text: uid
                        visible: false
                        font.pixelSize: Qt.application.font.pixelSize*1.2
                    }
                }
                Rectangle {
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.2
                    Layout.preferredWidth: listView.width*0.2
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 180
                    Text {
                        text: email
//                        width: 180
                        anchors.centerIn: parent
                        //anchors.fill: parent
                        //anchors.right: parent.right
                        font.pixelSize: Qt.application.font.pixelSize*1.2
                    }
                }
                Rectangle {
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.5
                    Layout.preferredWidth: listView.width*0.5
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 180
                    ComboBox {
                        id: roleCombo
                        currentIndex: role
                        font.pixelSize: Qt.application.font.pixelSize*1.2
                        model: roleModel
                        textRole: "text"
                        anchors.fill: parent
                        width: parent.width*0.6

                        onCurrentIndexChanged: {
                            if (roleModel.get(roleCombo.currentIndex).role === role)
                                return
                            changeUserRole(email, dataset_id, roleModel.get(roleCombo.currentIndex).role)
                            role = roleModel.get(roleCombo.currentIndex).role
                        }

                        //enabled: role !== 0
                    }
//                    Text {
//                        text: role_text
////                        width: 180
//                        anchors.centerIn: parent
//                                    //anchors.centerIn: parent
//                        //anchors.fill: parent
//                        //anchors.right: parent.right
//                        font.pixelSize: Qt.application.font.pixelSize*1.2
//                    }
                }

                //}
                Rectangle {
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.1
                    Layout.preferredWidth: listView.width*0.1
                    Layout.preferredHeight: 80
                    color: "lightyellow"
//                    height: 80
//                    width: 80
                    Button {
                        width: parent.height*0.6
                        height: parent.height*0.6
                        anchors.centerIn: parent
                        ToolTip.delay: 1000
                        ToolTip.timeout: 5000
                        ToolTip.visible: hovered
                        ToolTip.text: qsTr("删除")
                    //anchors.centerIn: parent
                    //text: "delete"
                        background: Image {
                            source: "/images/images/delete.jpg"

                            anchors.centerIn: parent
                            width: parent.height*0.9
                            height: parent.height*0.9
                            opacity: parent.hovered?0.3:1
                        }

//                    icon.source:  "/images/images/delete.jpg"
//                    icon.height: parent.height*0.7
//                    icon.width: parent.height*0.7
//                    icon.color: transparent
                        flat: true
                        onClicked: {
                            delUserDialog.grant_id = uid
                            delUserDialog.dataset_id = dataset_id
                            delUserDialog.open()
                        }
                        //enabled: role !== 0
                    }
                }
                Rectangle {
                    Layout.fillWidth: true
                    Layout.minimumWidth: listView.width*0.2
                    Layout.preferredWidth: listView.width*0.2
                    Layout.preferredHeight: 80
                    color: "lightyellow"
                }
//                MouseArea {
//                    hoverEnabled: true
//                    anchors.fill: parent
//                    onEntered: {

//                    }
//                    onExited: {

//                    }
//                }
            }
            Rectangle {
                anchors.bottom: parent.bottom
                height: 1
                width: parent.width
                color: "black"
            }
            //spacing: 30
            highlighted: false
            hoverEnabled: true
            onClicked:{
                        //listView.currentIndex = index
                console.log("clicked:", uid)
            }
        }
    }

//    Binding {
//        target: listView.pswitch
//        property: "checked"
//        value: m_pb
//    }
}
